gitignore チートシート
#gitignore
gitignoreの基本ルールをいっつも忘れるので、ちゃんと覚えておくonigiri.w2.icon
gitignoreとは?とかいう説明は省く
1. 行末の「/」をつけるとディレクトリと判別される
code: .gitignore
same_name/ # ディレクトリのみ
code: dir
root/
- sample_name # not除外対象
- sample_name/ # 除外対象
- sample.txt
注意.icon 「/」を行末に付けない場合は、ファイルのみ除外されるわけではない
付けない場合は、ファイル・ディレクトリの両方に当てはまるので注意。
code: .gitignore
same_name # ファイル・ディレクトリの両方とも除外対象になる
2. 行頭に「/」を付けると、gitignoreと同じ階層基準でファイル・ディレクトリを指定できる
code: .gitignore
/sample.html
/sam_dir/
code: dir
root/
- .gitignore
- sample.html # 除外対象
- directory/
- sample.html # not除外対象
- sample_dir/ # not除外対象
- bogeboge
- sample_dir/ # 除外対象
- hogehoge
3. 行頭に「/」を付けない場合、階層無視で一致したファイル・ディレクトリを全て除外
code: .gitignore
sample.html
code: dir
root/
- .gitignore
- sample.html # 除外
- hoge/
- sample.html # 除外
4. 特定の拡張子を除外したいなら、「*.拡張子」と指定すればいい
code: .gitignore
*.jpg
code: dir
root/
- .gitignore
- sample.jpg
- img/
- hoge.jpg
- boge.jpg
- bababa/
- babi.jpg
.jpg拡張子のファイルは全て除外される
5. gitignoreは同階層より親の階層には影響を及ぼさない
code: .gitignore
sample.txt
code: dir
root/
- sample.txt # not 除外対象
- hoge/
- .gitignore
- sample.txt # 除外対象
6. あるフォルダ内の特定のファイル/フォルダを除外したい場合
code: .gitignore
package/**/*.jpg
code: dir
root/
- A.jpg # not除外
- package/
- hoge/
- B.jpg # 除外
- boge/
- D.jpg # 除外
- C.jpg # 除外
もしpackage/*.jpgと指定してしまうと、C.jpgしか除外対象にならないので注意
*は「/以外の文字列に一致する」と知ってれば納得できる
参考
【Git】.gitignoreの書き方|バージョン管理しないファイルを除外する|Webエンジニア Wiki
.gitignore の書き方 - Qiita
gitignore/Python.gitignore at main · github/gitignore